var orderPayApp = angular.module("orderPayApp",[]);
orderPayApp.controller("orderPayController",function($scope,$http,scCodeService,$location,$state,Popup,$stateParams){
	//订单价格数据处理，从2处理成2.00格式
	$scope.intToFloat=function(num){
		num=Math.round(num*100)/100;
		var obj=num.toString().split(".");
		if(obj.length==1){
			num=num+'.00';
		}else{
			if(obj[1].length==1){
				num=num+'0';
			}
		}
		return num;
		
	}
	//订单数据显示处理方法
	$scope.orderShow=function(data){
		//判断订单状态，未支付则停留在此页，已支付则跳转至订单完成界面
		if(data.status=="7"){
			$location.path("/orderDetail/"+data.chargeDetail.orderNo);
		}
		//计算充电费和服务费，优惠
		$scope.chargePrice=0;
		$scope.servicePrice=0;
		$scope.discountPrice=0;
		$scope.countPrice=0;
		$scope.chargeList=data.orderDetails;
		
		angular.forEach($scope.chargeList, function(data,index,array){
			if(array[index].serviceNo=="100002"){
				
				$scope.chargePrice=parseFloat($scope.chargePrice+array[index].totalPrice);
			}else if(array[index].serviceNo=="100001"){
				
				$scope.servicePrice=parseFloat($scope.servicePrice+array[index].totalPrice);
			}
			$scope.countPrice=parseFloat($scope.countPrice)+parseFloat(array[index].totalPrice);
			$scope.discountPrice=parseFloat($scope.discountPrice)+parseFloat(array[index].totalPrice-array[index].realTotalPrice);
		});
		$scope.chargePrice=$scope.intToFloat($scope.chargePrice);
		$scope.servicePrice=$scope.intToFloat($scope.servicePrice);
		$scope.countPrice=$scope.intToFloat($scope.countPrice);
		//判断优惠金额是否有值，有则显示，无则隐藏
		$scope.hasDiscount=false;
		if($scope.discountPrice && $scope.discountPrice>0){
			$scope.hasDiscount=true;
			$scope.discountPrice=$scope.intToFloat($scope.discountPrice);
		}
		
		//将订单数据存储到本地localStorage里
		/*var ls=localStorage;
	    //0
	    ls.removeItem('chargeDetail');
	    //清除存入数据
	    ls.setItem('userinfo',JSON.stringify($scope.userinfo));
	    ls.setItem('chargeDetail',JSON.stringify(data));
	    //显示存入数据*/
	}
	//获取路由上的订单id调用接口获取订单详情
	$scope.getOrderDetailByOrderNo=function(){
		//获取路由上获取订单id
		if($stateParams.orderNo){
			$scope.orderNo=$stateParams.orderNo;
		}
		
		//根据订单号查询订单详细
		//$scope.appserver='http://192.168.6.112:8080/appserver/';
		$scope.appserver='http://localhost:8080/appserver/';
		/*$http({
			method:'get',
			url:$scope.appserver+'fund/fundDetail',
			params:{orderNo:$scope.orderNo}
		}).success(function(data) {
			
			if(data.data){
				$scope.order=data.data;
				
				//调用订单显示数据处理方法
				$scope.orderShow($scope.order);
			}
		});*/
		var postData = {
			orderNo:$scope.orderNo
		}
		scCodeService.getOrderDetail(postData).then(function(data){
				
				if(data.code == "200"){
					 if(data.data){
						$scope.order=data.data;
						
						//调用订单显示数据处理方法
						$scope.orderShow($scope.order);
					}
				}
			})
	}
	$scope.getOrderDetailByOrderNo();
	//获取支付接口参数
	$scope.getPayParams=function(){
		document.getElementById("loadingToast").style.display = "block";
		var openId=localStorage.getItem("openId");
		//获取支付接口参数
		var postData = {
			orderNo:$scope.orderNo,
			payType:"weixin",
			appId:124,
			body:'华体照明-路灯充电服务',
			openId:openId
		}
		scCodeService.getPayParams(postData).then(function(data){
				/*var result = JSON.stringify(data);
            	alert(result);*/
				
				document.getElementById("loadingToast").style.display = "none";
				if(data.code == "200"){
					if(data.data){
						$scope.wxParams = data.data.bizData;
						if($scope.wxParams.returnCode=="SUCCESS"){
							//显示存入数据
					    	$scope.callpay(); //调用支付插件
						}else{
							Popup.notice($scope.wxParams.msg, 3000, function() {
                            	
                    		});
						}
					    
					}
				}else{
					Popup.notice('请求失败', 3000, function() {
                            
                    });
				}
			})
	}
	//生成32位随机数，微信支付参数用
	$scope.generateMixed=function() {
	     $scope.chars = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'];
	     var charsres='';
	     for(var i = 0; i < 32 ; i ++) {
	         var id = Math.ceil(Math.random()*35);
	         charsres += $scope.chars[id];
	     }
	     return charsres;
	}
	//调用微信JS api 支付
	$scope.jsApiCall=function(){
		//传入参数，调用微信支付插件
		WeixinJSBridge.invoke(
			'getBrandWCPayRequest',
			 {
		           "appId":$scope.wxParams.appId, //公众号名称，由商户传入 ,$scope.wxParams.appId,//    
		           "timeStamp":$scope.wxParams.timeStamp,         //时间戳，自1970年以来的秒数     $scope.wxParams.timeStamp,//
		           "nonceStr":$scope.wxParams.nonceStr, //随机串     $scope.wxParams.nonceStr,//
		           "package":$scope.wxParams.packageStr,    // $scope.wxParams.package,//
		           "signType":"MD5", 
		           "total_fee":"80",        //微信签名方式：     
		           "paySign":$scope.wxParams.paySign //微信签名 $scope.wxParams.paySign,//
		     },
			function(res){
				WeixinJSBridge.log(res.err_msg);
				
				//alert(res.err_code+res.err_desc+res.err_msg);
				//alert(res.err_msg);
				if(res.err_msg == "get_brand_wcpay_request:ok") {
					//成功回调
					//$location.path("/myOrder");
					//alert("ok");
					window.location.href=htConfig.hostUrl+"index.html?#/myOrder";
				}else{
					Popup.notice(res.err_msg, 3000, function() {
                    
                    });
				}
			}
		);
	}
	//检测浏览器是否支持微信JS-SDK
	$scope.callpay=function(){
		if (typeof WeixinJSBridge == "undefined"){
		    if( document.addEventListener ){
		        document.addEventListener('WeixinJSBridgeReady', $scope.jsApiCall(), false);
		    }else if (document.attachEvent){
		        document.attachEvent('WeixinJSBridgeReady', $scope.jsApiCall()); 
		        document.attachEvent('onWeixinJSBridgeReady', $scope.jsApiCall());
		    }
		}else{
		    $scope.jsApiCall();
		}
	}
    
})